/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.basedata.subject.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.enums.DelFlagEnums;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.common.site.modules.sys.utils.UserUtils;
import com.smooth.dao.gen.BaseSchSubjectMapper;
import com.smooth.dao.gen.bean.BaseCollege;
import com.smooth.dao.gen.bean.BaseSchSubject;
import com.smooth.dao.gen.bean.BaseSchSubjectExample;
import com.smooth.site.sx.mag.basedata.college.service.BaseCollegeService;

/**
 * 学科Service
 * @author 北冥
 * @version 2019-02-25
 */
@Service
@Transactional(readOnly = true)
public class BaseSchSubjectService{
	Logger log = LoggerFactory.getLogger(BaseSchSubjectService.class);
	@Autowired
	BaseSchSubjectMapper baseSchSubjectMapper;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	BaseCollegeService baseCollegeService;
	public BaseSchSubject get(String no) {
		return baseSchSubjectMapper.selectByPrimaryKey(no);
	}
	
	public List<BaseSchSubject> findList(BaseSchSubject baseSchSubject) {
		BaseSchSubjectExample baseSchSubjectExample = new BaseSchSubjectExample();
		BaseSchSubjectExample.Criteria criteria = baseSchSubjectExample.createCriteria();
		
		return baseSchSubjectMapper.selectByExample(baseSchSubjectExample);
	}
	
	public Page<BaseSchSubject> findPage(Page<BaseSchSubject> page, BaseSchSubject baseSchSubject) {
		BaseSchSubjectExample baseSchSubjectExample = new BaseSchSubjectExample();
		BaseSchSubjectExample.Criteria criteria = baseSchSubjectExample.createCriteria();
		
		if(UserUtils.isSchMag() ) {
			criteria.andSchIdEqualTo(UserUtils.getUser().getSchId());
		}
		baseSchSubjectExample.setPage(page);
		
		List<BaseSchSubject> dlist = baseSchSubjectMapper.selectByExample(baseSchSubjectExample);
		page.setList(dlist);
		page.setCount(baseSchSubjectMapper.countByExample(baseSchSubjectExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public BaseSchSubject save(BaseSchSubject baseSchSubject) {
		if(StringUtils.isNotBlank(baseSchSubject.getSchId())) {
			BaseCollege baseCollege = baseCollegeService.get(baseSchSubject.getSchId());
			baseSchSubject.setSchName(baseCollege.getCollegeName());
		}
		if(StringUtils.isNotEmpty(baseSchSubject.getId())&&get(baseSchSubject.getId())!=null) {
			int optNum = baseSchSubjectMapper.updateByPrimaryKeySelective(baseSchSubject);
			if(optNum==1) {
				log.info("更新 student 数据【"+baseSchSubject.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+baseSchSubject.toString()+"】失败");
			}
			return baseSchSubjectMapper.selectByPrimaryKey(baseSchSubject.getId());
		}else {
			Date date = new Date();
			baseSchSubject.setCreateDate(date);
			baseSchSubject.setDelFlag(DelFlagEnums.DEL_FLAG_NORMAL.value);
			baseSchSubject.setId(idGenerater.genClsNo("baseSchSubject"));
			baseSchSubject.setUpdateDate(date);
			int optNum =  baseSchSubjectMapper.insertSelective(baseSchSubject);
			if(optNum==1) {
				log.info("插入 student 数据【"+baseSchSubject.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+baseSchSubject.toString()+"】失败");
			}
			return baseSchSubject;
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BaseSchSubject baseSchSubject) {
		baseSchSubjectMapper.deleteByPrimaryKey(baseSchSubject.getId());
	}
	
}